package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.DishService;
import com.sky.service.SetmealService;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * ClassName: ShoppingCartServiceImpl
 * Package:
 * Description
 * 添加购物车
 *
 * @Author xw
 * @Create 2023/11/23 20:06
 */
@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {


    /*
     * 购物车
     * */
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    /*
     * 菜品
     * */
    @Autowired
    private DishService dishService;

    /*
     * 套餐
     * */
    @Autowired
    private SetmealService setmealService;

    /**
     * 添加购物车
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        // 判断当前加入购物车中的商品是否已经存在了
        ShoppingCart shoppingCart = new ShoppingCart();
        // 拷贝
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        // 用户id
        Long userid = BaseContext.getCurrentId();
        shoppingCart.setUserId(userid);
        // sql 查询
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);


        //如果已经存在了，只需要将数量加一
        if (!list.isEmpty()) {
            ShoppingCart shoppingCart1 = list.get(0);
            // 原有数据+1
            shoppingCart1.setNumber(shoppingCart1.getNumber() + 1); // update shopping cart set number = ? where id = ?

            shoppingCartMapper.UpdateNumberByid(shoppingCart1);
        } else {
            //如果不存在，需要插入一条购物车数船

            // 判断本次添加到购物车的是菜品还是套餐
            Long dishId = shoppingCartDTO.getDishId();
            Long setmealId = shoppingCartDTO.getSetmealId();

            if (dishId != null) {
                // 添加菜品
                // 查询菜品表
                DishVO byId = dishService.getById(dishId);
                shoppingCart.setName(byId.getName()); // 菜名
                shoppingCart.setImage(byId.getImage()); // 图片
                shoppingCart.setAmount(byId.getPrice()); // 价格

            } else {
                // 套餐
                log.info("套餐");
                SetmealVO setmealVO = setmealService.getById(setmealId);
                shoppingCart.setName(setmealVO.getName()); // 菜名
                shoppingCart.setImage(setmealVO.getImage()); // 图片
                shoppingCart.setAmount(setmealVO.getPrice()); // 价格

            }

            shoppingCart.setNumber(1); //数量
            shoppingCart.setCreateTime(LocalDateTime.now()); // 时间
            // 插入
            shoppingCartMapper.insert(shoppingCart);


        }

    }

    /**
     * 查看购物车
     */
    @Override
    public List<ShoppingCart> showShoppingCart() {
        // 用户id
        Long userid = BaseContext.getCurrentId();
        log.info("查看购物车结果 id：{}", userid);
        ShoppingCart build = ShoppingCart.builder()
                .userId(userid)
                .build();

        log.info("查看购物车结果：{}", build);

        return shoppingCartMapper.showShoppingCart(build);
    }

    /**
     * 清空购物车
     */
    @Override
    public void cleanShoppingCart() {
        Long userid = BaseContext.getCurrentId();
        shoppingCartMapper.cleanShoppingCart(userid);
    }


    /**
     * 删除购物车中一个商品
     */
    @Override
    public void deleteNoeShoppingCart(ShoppingCartDTO shoppingCartDTO) {

        // 拷贝
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);

        // 用户
        Long userid = BaseContext.getCurrentId();
        shoppingCart.setUserId(userid);

        if (shoppingCart.getDishId() != null) {
            // 菜品
            deleteNoeShoppingCart(shoppingCart);
        } else {
            // 套餐
            deleteNoeShoppingCart(shoppingCart);
        }

    }


    /*
     * 删除单个方法
     * */
    public void deleteNoeShoppingCart(ShoppingCart shoppingCart) {
        Integer number = shoppingCartMapper.showNumber(shoppingCart);
        if (number > 1) {
            shoppingCart.setNumber(number - 1); //数量
            shoppingCart.setCreateTime(LocalDateTime.now()); // 时间
            // 插入
            shoppingCartMapper.update(shoppingCart);
        } else {
            shoppingCartMapper.deleteNoeShoppingCart(shoppingCart);
        }
    }
}
